﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using PeoplesJukebox.Web.ServiceClients;
using PeoplesJukebox.Web.Enums;
using PeoplesJukebox.Web.Bases;
using System.Windows.Threading;

namespace PeoplesJukebox.Web.Pages
{
    /// <summary>
    /// Base class for application pages
    /// </summary>
    public class PageBase : UserControlBase
    {
        private DispatcherTimer _timeout;

        /// <summary>
        /// Signifies that the page has been successfully completed
        /// </summary>
        /// <param name="nextPage">Page type to navigate to</param>
        public delegate void OnCompleteEventHandler(PageType nextPage);

        /// <summary>
        /// Signifies that the page has been cancelled
        /// </summary>
        public delegate void OnCancelledEventHandler();

        /// <summary>
        /// Notifies that the page has been successfully completed
        /// </summary>
        public event OnCompleteEventHandler Complete;

        /// <summary>
        /// Notifies that the page has been cancelled
        /// </summary>
        public event OnCancelledEventHandler Cancelled;

        /// <summary>
        /// Returns the page type
        /// </summary>
        public virtual PageType PageType { get { return PageType.Start; } }        

        /// <summary>
        /// Resets page state
        /// </summary>
        public virtual void Reset() 
        {
            //TODO - this is to reset the app to the homepage if
            //a user starts a selection without finishing. Not reliable

            //this._timeout = new DispatcherTimer();
            //this._timeout.Interval = new TimeSpan(0, 5, 0);
            //this._timeout.Tick += new EventHandler(_timeout_Tick);
            //this._timeout.Start();
        }

        /// <summary>
        /// Positive page completion - move to next page in workflow
        /// </summary>
        /// <param name="nextPage">Page type to move to</param>
        protected void Completed(PageType nextPage)
        {
            ResetTimer();
            if (Complete != null)
            {
                Complete(nextPage);
            }
        }

        /// <summary>
        /// Negative page completion - flag cancellation
        /// </summary>
        protected void Cancel()
        {
            ResetTimer();
            if (Cancelled != null)
            {
                Cancelled();
            }
        }

        private void _timeout_Tick(object sender, EventArgs e)
        {
            this.Complete(PageType.Home);
        }

        private void ResetTimer()
        {
            if (this._timeout != null)
            {
                this._timeout.Stop();
                this._timeout.Tick -= _timeout_Tick;
                this._timeout = null;
            }
        }
    }
}